import { prisma } from "@/lib/prisma"
import { Metadata } from "next"
import { SalesReport } from "@/components/reports/sales-report"

export const metadata: Metadata = {
  title: "销售统计 - 商场销售系统",
  description: "销售统计页面",
}

export default async function SalesReportPage() {
  // 获取销售数据
  const sales = await prisma.sale.findMany({
    include: {
      items: {
        include: {
          product: true,
        },
      },
    },
    orderBy: {
      createdAt: 'desc',
    },
  })

  // 计算总销售额和销售数量
  const totalSalesAmount = sales.reduce((sum, sale) => sum + sale.totalAmount.toNumber(), 0)
  const totalSalesCount = sales.reduce((count, sale) => count + sale.items.length, 0)

  return (
    <div className="space-y-4 p-8 bg-gray-50">
      <h1 className="text-2xl font-bold text-gray-900">销售统计</h1>
      <div className="bg-white p-6 rounded-lg shadow-lg border border-gray-200">
        <h2 className="text-xl text-gray-800 mb-4">统计信息</h2>
        <div className="flex justify-between">
          <div className="text-lg text-gray-700">总销售额:</div>
          <div className="text-2xl font-semibold text-green-600">¥{totalSalesAmount.toFixed(2)}</div>
        </div>
        <div className="flex justify-between mt-2">
          <div className="text-lg text-gray-700">总销售数量:</div>
          <div className="text-2xl font-semibold text-blue-600">{totalSalesCount}</div>
        </div>
      </div>
      <div className="bg-white p-4 rounded shadow mt-4">
        <SalesReport sales={sales} />
      </div>
    </div>
  )
} 